package com.fiberhome.ztelauncher.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.fiberhome.ztelauncher.R;
import com.fiberhome.ztelauncher.resource.ResourceManager;
import com.fiberhome.ztelauncher.uilib.GlideRoundTransform;

/**
 * Created by xc on 2018/3/22.
 * 描述：
 */

public class BitmapUitl {

    private static final String ASSETS_PATH = "file:///android_asset/";

    /**
     * @param context
     * @param view
     * @param url
     */
    public static void loadBackground(Context context, final View view, String url) {
        if(context == null){
            return;
        }
        RequestOptions options = new RequestOptions();
        Glide.with(context)
                .load(ResourceManager.getResourceManager().getResourceUrl(url))
                .apply(options)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        view.setBackgroundDrawable(resource);
                    }
                });
    }

    /**
     * @param context
     * @param view
     * @param url
     */
    public static void loadBackgroundRound(Context context, final View view, String url) {
        if(context == null){
            return;
        }
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transforms(new GlideRoundTransform(context));
        Glide.with(context)
                .load(ASSETS_PATH + url)
                .apply(options)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        view.setBackgroundDrawable(resource);
                    }
                });
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadBitmap(Context context, ImageView imageView, String url) {
        if(context == null){
            return;
        }
        RequestOptions options = new RequestOptions().centerInside();
        Glide.with(context)
                .load(ResourceManager.getResourceManager().getResourceUrl(url))
                .apply(options)
                .into(imageView);
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadBitmapRound(Context context, ImageView imageView, String url) {
        if(context == null){
            return;
        }
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transforms(new GlideRoundTransform(context));
        Glide.with(context)
                .load(ASSETS_PATH + url)
                .apply(options)
                .into(imageView);
    }

    /**
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadBitmapWH(Context context, ImageView imageView, String url,int width,int height) {
        if(context == null){
            return;
        }
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher_round)
                .override(width,height);
        Glide.with(context)
                .load(ASSETS_PATH + url)
                .apply(options)
                .into(imageView);
    }

    /**
     * @param context
     * @param tv
     * @param url
     */
    public static void loadLeft(Context context, final TextView tv, String url) {
        if(context == null){
            return;
        }
        Glide.with(context)
                .load(ResourceManager.getResourceManager().getResourceUrl(url))
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        tv.setCompoundDrawablesWithIntrinsicBounds(resource,null,null,null);
                    }
                });
    }
}
